package com.i2bt.app.files.model;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.files.model.FilesDownloadClient.onDownloadListener;
import com.i2bt.bean.FileInfoBean;
import com.i2bt.bean.FilesTransmissionBean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


public class FilesTransmissionManager implements onDownloadListener {
	
	private static FilesTransmissionManager fileTtransmissionManager;
	
	private static Queue<FilesDownloadClient> downloadClientQueue;
	
	private static List<FilesTransmissionBean> downloadFilesTranData;
	
	private FilesTransmissionManager() {
		downloadClientQueue = new LinkedList<FilesDownloadClient>();
	}
	
	public static FilesTransmissionManager getInstance() {
		if (fileTtransmissionManager == null) {
			fileTtransmissionManager = new FilesTransmissionManager();
		}
		return fileTtransmissionManager;
	}
	
	/**
	 * 添加下载任务
	 * @param activity
	 * @param ip 地址
	 * @param port 端口
	 * @param fileInfoBean
	 */
	public void addDownloadTask(BaseActivity activity, String ip, int port, FileInfoBean[] fileInfoBean) {
//		FilesDownloadClient downloadClient = new FilesDownloadClient(activity,
//				ip, port, wrapFilesTran(fileInfoBean));
		FilesDownloadClient downloadClient = new FilesDownloadClient(
				activity, ip, port, wrapFilesTran(fileInfoBean), this);
		
		if (downloadClientQueue.size() == 0) {
			downloadClientQueue.add(downloadClient);
			downloadClient.start();
		}
		else {
			downloadClientQueue.add(downloadClient);
		}
	}
	
	/**
	 * 添加上传任务
	 * @param activity
	 * @param ip
	 * @param port
	 * @param fileInfoBean
	 */
	public void addUploadTask(BaseActivity activity, String ip, int port, FileInfoBean[] fileInfoBean) {
		
	}
	
	/**
	 * 把文件信息类打包为文件传输信息类
	 * @param fileInfoBean
	 * @return
	 */
	private FilesTransmissionBean[] wrapFilesTran(FileInfoBean[] fileInfoBean) {
		FilesTransmissionBean[] transmissionBean = new 
				FilesTransmissionBean[fileInfoBean.length];
		for (int i = 0; i < fileInfoBean.length; i++) {
			transmissionBean[i] = new FilesTransmissionBean();
			transmissionBean[i].setFileName(fileInfoBean[i].getFileName());
			transmissionBean[i].setFileLength(fileInfoBean[i].getFileLength());
			transmissionBean[i].setFile(fileInfoBean[i].isFile());
		}
		return transmissionBean;
	}

	/**
	 * 刷新下载数据
	 */
	private void refreshDownloadFilesData() {
		if (downloadFilesTranData == null) {
			downloadFilesTranData = new ArrayList<FilesTransmissionBean>();
		}
		
		downloadFilesTranData.clear();
		for (int i = 0; i < downloadClientQueue.size(); i++) {
		    Iterator<FilesDownloadClient> it = downloadClientQueue.iterator();
			if (it.hasNext()) {
				FilesDownloadClient filesDownloadClient = it.next();
				FilesTransmissionBean[] filesTransmissionBeans = 
						filesDownloadClient.getTransmissionBeans();
				
				for (int j = 0; j < filesTransmissionBeans.length; j++) {
					FilesTransmissionBean bean = filesTransmissionBeans[i];
					downloadFilesTranData.add(bean);
				}
			}
		}
 		
		System.out.println(downloadFilesTranData.toString());
	}
	
	@Override
	public void onDownloadUpdate() {
		refreshDownloadFilesData();
	}

	@Override
	public void onDownloadResult() {
		downloadClientQueue.poll();
		if (downloadClientQueue.size() > 0) {
			FilesDownloadClient downloadClient = downloadClientQueue.poll();
			downloadClient.start();
		}
	}
	
}
